//
//  AYWelcomeViewController.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/14.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class AYWelcomeViewController: UIViewController {

    /**
     视图载入完成的时候把约束做好
     */
    override func viewDidLoad() {
        super.viewDidLoad()
        
        preparUI()
        
        setIcon()
        
        AYUserAccountViewModel.sharedInstance.loadUserInfo { () -> () in
            self.setIcon()
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
        
    }
    
    private func setIcon() {
        if let avatar_large = AYUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
    }
    
    private func moveAnimation() {
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                AMPrint(items: "动画完成")
                UIView.animateWithDuration(0.5, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        appDelegate.switchRootViewController(AYMainViewController())
                })
        }
    }
    
    /**
     布局子控件、设置UI
     */
    private func preparUI() {
        
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        
        bkgView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.width.height.equalTo(85.0)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.iconView.snp_bottom).offset(16)
        }
    }
    
    
    // MARK: - lazy
     /// 背景图片
    private var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
        /// 头像
    private var iconView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
        /// 消息label
    private var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(15)
        label.alpha = 0
        label.sizeToFit()
        
        return label
    }()
    
}
